package com.ruoyi.system.service.impl;

import com.ruoyi.system.domain.Constructionsection;
import com.ruoyi.system.domain.Project;
import com.ruoyi.system.domain.Projects;
import com.ruoyi.system.domain.ProjectsTwo;
import com.ruoyi.system.mapper.AppliDetailMapper;
import com.ruoyi.system.mapper.ProjectsMapper;
import com.ruoyi.system.mapper.PurchaseMapper;
import com.ruoyi.system.service.IProjectsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.text.DecimalFormat;
import java.util.List;
import java.util.Map;

/**
 * 项目Service业务层处理
 *
 * @author ruoyi
 * @date 2020-12-16
 */
@Service
public class ProjectsServiceImpl implements IProjectsService {
    @Resource
    private ProjectsMapper projectsMapper;

    @Autowired
    private PurchaseMapper purchaseMapper;

    @Autowired
    private AppliDetailMapper appliDetailMapper;

    /**
     * 查询项目
     *
     * @param pId 项目ID
     * @return 项目
     */
    @Override
    public Projects selectProjectsById(Long pId) {
        return projectsMapper.selectProjectsById(pId);
    }

    /**
     * 查询项目列表
     *
     * @param projects 项目
     * @return 项目
     */
    @Override
    public List<Projects> selectProjectsList(Projects projects) {
        List<Projects> list = projectsMapper.selectProjectsList(projects);
        DecimalFormat df = new DecimalFormat("######0.00");
        df.setRoundingMode(RoundingMode.HALF_UP);
        for (int i = 0; i < list.size(); i++) {
            Long getpId = list.get(i).getpId();
//            Double selectMoney=projectsMapper.selectMoney(getpId);
//            Double selectSum=projectsMapper.selectSum(getpId);
            Double result = projectsMapper.selectCountMatSum(getpId);
            Double cost = appliDetailMapper.projectCostById(getpId);
            Double shipping = purchaseMapper.getShipping(getpId);
//            //材料费用
//            if (null==selectMoney){
//                selectMoney = 0D;
//            }
            //材料费用
            if (null == result) {
                result = 0D;
            }
            //运费
            if (null == shipping) {
                shipping = 0D;
            }
            //租赁费用
            if (null == cost) {
                cost = 0D;
            }
            //总成本赋值
            Double result1 = Double.valueOf(df.format(result));
            Double sum = Double.valueOf(df.format(new BigDecimal(result1 + cost + shipping + "")));
            list.get(i).setpCost(sum + "=材料成本:" + result1 + "+租赁成本:" + cost + "+运费:" + shipping);
        }
        return list;
    }

    /**
     * 新增项目
     *
     * @param projects 项目
     * @return 结果
     */
    @Override
    public int insertProjects(ProjectsTwo projects) {
        //先查询是否有同名
        if (projectsMapper.selectpName(projects.getpNumber(), projects.getpName()) <= 0) {
            //添加
            projectsMapper.insertProjects(projects);
            //判断是否有班组 添加
            if (projects.getConstructionsections().size() > 0) {
                for (int i = 0; i < projects.getConstructionsections().size(); i++) {
                    projectsMapper.addconstructionsection(projects.getpId(), projects.getConstructionsections().get(i));
                }
            }
        } else {
            return 500;
        }
        return 1;
    }

    /**
     * 删除项目对象
     *
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Override
    public int deleteProjectsByIds(long ids) {   //项目内是否有材料
        if (projectsMapper.selectProjectsMateial(ids) <= 0) {
            projectsMapper.deleteConstructionsection(ids);
            return projectsMapper.deleteProjectsByIds(ids);
        } else {
            return 0;
        }
    }

    /**
     * 删除项目信息
     *
     * @param pId 项目ID
     * @return 结果
     */
    @Override
    public int deleteProjectsById(Long pId) {
        return projectsMapper.deleteProjectsById(pId);
    }

    public List<Constructionsection> selectconstructionsections() {
        return projectsMapper.selectconstructionsections();
    }

    //编辑
    @Override
    public int updateProject(Project projects) {
        //修改项目信息
        if (projectsMapper.selectpNameByid(projects.getpNumber(), projects.getpName(), projects.getpId() + "") <= 0) {
            int i = projectsMapper.updateProject(projects);
            //根据项目id删除所有id
            int a = projectsMapper.deleteConstructionsection(projects.getpId());
            int result = 0;
            //根据新给的班组插入所有id
            List<Integer> constructionsections = projects.getConstructionsections();
            for (int i1 = 0; i1 < constructionsections.size(); i1++) {
                //插入所有班组id
                result = projectsMapper.insertConstructionsectionIds(constructionsections.get(i1), projects.getpId());
            }
            if (i >= 0 && a >= 0 && result >= 0) {
                return 1;
            } else {
                return 0;
            }
        } else {
            return 0;
        }
    }

}
